<template>
	<view>
		<view v-if="inPorcessOrderId != null" class="order-process" @click="toOrder">您有进行中的订单,点击查看</view>

		<map id="map" :longitude="longitude" :latitude="latitude" :style="contentStyle" scale="15"
			:enable-traffic="false" :show-location="true" :enable-poi="true" class="map">
			<cover-image class="location" src="../../static/workbench/location.png"
				@tap="toLocationHandle()"></cover-image>
		</map>
		<view class="panel">
			<view class="from" @tap="chooseLocationHandle('from')">
				<text>{{ from.address }}</text>
			</view>
			<view class="dashed-line"></view>
			<view class="to" @tap="chooseLocationHandle('to')">
				<text>{{ to.address }}</text>
			</view>
			<button class="btn" @tap="toCreateOrderHandle">呼叫代驾</button>
		</view>
	</view>
</template>

<script>
	import GtPush from '../../lib/gtpush-min.js'
	//地图点选插件
	const chooseLocation = requirePlugin('chooseLocation');
	let QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
	let qqmapsdk;

	export default {
		data() {
			return {
				getui:{
					appid: 'lShzj30HeZ63w31cC0H2q4',
					// appid: '__UNI__C23DA4B',
					// appid: '__UNI__A4AD2D8',
					cid: '',
					online: false,
					messages: ''
				},
				
				//开始位置
				from: {
					address: '请输入你的代驾地点',
					longitude: 0,
					latitude: 0
				},
				//结束位置
				to: {
					address: '输入你的目的地',
					longitude: 0,
					latitude: 0
				},
				//当前位置经纬度
				longitude: 104.06163,
				latitude: 30.59415,
				//窗口样式
				contentStyle: '',
				windowHeight: 0,
				//地图对象
				map: null,
				//标记:开始位置点选或者结束位置点选
				chooseFromOrToflag: null,
				inPorcessOrderId: null,
				type: ''
			};
		},
		methods: {
			// 初始化个推， 推送SDK
			initGeTui(){
				let _this = this;
				//打开调试模式，调试模式下将会向控制台输出SDK⽇志，正式上线时建议关掉
				GtPush.setDebugMode(true)
				//初始化推送SDK
				  GtPush.init({
				    appid: _this.getui.appid,
				    onClientId: (res) => {
				      //个推ClientId回调，拿到cid后将其上报到服务端
				      console.info(`onClientId: ${res.cid}`)
					  _this.post("/common/getui/bind/"+res.cid,(res)=>{
					  })
					  
				  },
				
				  onlineState: (data) => {
				    //个推ClientId在线状态回调
				    console.log(`onlineState: ${data.online}`)
				  },
				
				  onPushMsg: (res) => {
				    //推送消息回调
				    console.log(`推送消息回调`, res.message)
					let message =JSON.parse(res.message)
					console.log(message);
					if	(message && message.messageType == 1){
						uni.showModal({
							title: message.message,
							success: function (res) {
									if (res.confirm) {
										setTimeout(()=>{
											uni.navigateTo({
												url: "/pages/order/order?orderNo=" + message.orderNo
											})
										},1000)
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
						})
						
					}
				  },
				
				  onError: (res) => {
				    //错误回调
				    console.log(`onError`, res.error)
				  }
				})
			},
			
			// 加载乘客进行中的订单
			loadInProcessOrder() {
				let _this = this;
				_this.get("/order/app/order/customer/inprocess", (res) => {
					console.log(res);
					let {
						success,
						data
					} = res.data;
					if (success && data) {
						console.log(data.orderNo);
						// 如果有进行中的订单则 跳转创建订单页 create_order.vue
						if (data.status == _this.orderStatus.ORDER_STATUS_WAIT) {
							uni.navigateTo({
								url: "/pages/create_order/create_order"
							})
						} else if (data.status == _this.orderStatus.ORDER_STATUS_ACCEPTED ||
							data.status == _this.orderStatus.ORDER_STATUS_ARRIVE ||
							data.status == _this.orderStatus.ORDER_STATUS_START_DRIVING) {
							// 如果是 1接单  2司机到达 3开始代驾  则跳转司乘同显
							uni.navigateTo({
								url: "/pages/move/move?orderNo=" + data.orderNo
							})
						} else if(data.status == _this.orderStatus.ORDER_STATUS_ENSURE ||
							data.status == _this.orderStatus.ORDER_STATUS_NOT_PAY) {
							// 如果是 4结束代驾，5.司机确认费用，6未付款 , 则跳转订单详情页
							uni.navigateTo({
								url: "/pages/order/order?orderNo=" + data.orderNo
							})
						}
			
					}
				})
			},
			
			// 呼叫代驾
			toCreateOrderHandle() {
				let _this = this;
				if (_this.to.address != "输入你的目的地") {
					uni.setStorageSync("to",_this.to);
					uni.setStorageSync("from",_this.from);
					setTimeout(()=>{
						uni.navigateTo({
							url: "/pages/create_order/create_order"
						})
					}, 2000)
				} else {
					uni.showToast({
						title: "请选择您的目的地",
						icon: "error"
					})
				}

			},
			// 地图点选
			chooseLocationHandle(type) {
				let _this = this;
				_this.type = type
				const referer = '飞驰专车'; //调用插件的app的名称
				wx.navigateTo({
					url: 'plugin://chooseLocation/index?key=' + _this.Consts.QQMAP_KEY + '&referer=' + referer
				});
			},
			//初始化窗口样式
			initStyle() {
				let _this = this;
				//处理窗口样式
				let windowHeight = uni.getSystemInfoSync().windowHeight;
				_this.windowHeight = windowHeight;
				_this.contentStyle = `height:${_this.windowHeight}px;`;
			},
			// 初始化地图
			initMap() {
				let _this = this;
				_this.map = uni.createMapContext("map", this)
				_this.getLocation()

			},
			// 获取腾讯地图中密钥Key
			initQQMapWx() {
				let _this = this;
				qqmapsdk = new QQMapWX({
					key: _this.Consts.QQMAP_KEY // 必填
				});
			},
			// 获取当前位置经纬度
			getLocation() {
				let _this = this;
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						_this.latitude = res.latitude;
						_this.longitude = res.longitude;
						// 开始纬度
						_this.from.latitude = res.latitude;
						// 开始精度
						_this.from.longitude = res.longitude
						let location = {
							latitude: _this.from.latitude,
							longitude: _this.from.longitude
						}
						qqmapsdk.reverseGeocoder({
							location: location,
							success: function(res) {
								console.log("地址解析为");
								console.log(res);
								let address = res.result.address;
								// 获取当前开始代驾地点
								_this.from.address = address;
							}
						});
					},
					fail: function(error) {
						uni.showToast({
							icon: "error",
							title: "定位失败"
						})
					}
				});
			},
			// 点击按钮获取当前位置
			toLocationHandle() {
				let _this = this;
				_this.map.moveToLocation({
					longitude: _this.longitude,
					latitude: _this.latitude,

				})
			},
		},
		onShow: function() {
			let _this = this;
			// 如果点击确认选点按钮，则返回选点结果对象，否则返回null
			const location = chooseLocation.getLocation()
			if (_this.type == "to" && location != null) {
				_this.to.address = location.name;
				_this.to.longitude = location.longitude;
				_this.to.latitude = location.latitude;
			} else if (_this.type == "from" && location != null) {
				_this.from.address = location.name;
				_this.from.longitude = location.longitude;
				_this.from.latitude = location.latitude;
			}

		},
		onHide: function() {},
		onLoad: function() {
			let _this = this;
			// 初始化地图
			_this.initMap()
			// 获取腾讯地图中的密钥
			_this.initQQMapWx();
			_this.initStyle();
			// 加载乘客进行中的订单
			_this.loadInProcessOrder();
			// 初始化个推
			_this.initGeTui()
		},
		onUnload: function() {}
	};
</script>

<style lang="less">
	@import url('workbench.less');
</style>